/**   
= * Copyright © 2016 北京之洲科技有限责任公司. All rights reserved.
 * 
 * @Title: MainTemp.java 
 * @Prject: carfactory
 * @Package: com.zizo.main 
 * @Description: 
 * @author: Alexis  
 * @email heyan0227@outlook.com
 * @date: 2016年7月23日 下午2:38:08 
 * @version: V1.0   
 */
package com.zizo.main;

import java.io.File;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.zizo.autohome.bbs.crawl.BBSCrawlOfAutoHomeByCycle;
import com.zizo.autohome.bbs.dao.BBSDAOOfAutoHome;
import com.zizo.autohome.bbs.zlfk.crawl.ZLPJCrawlOfAutoHome;
import com.zizo.autohome.bbs.zlfk.dao.ZLFKDaoOfAutoHome;
import com.zizo.autohome.carinfo.crawl.CarModelCrawlOfAutoHome;
import com.zizo.autohome.carinfo.dao.CarModelDao;
import com.zizo.autohome.choice.crawl.ChoiceCrawlOfAutoHome;
import com.zizo.autohome.choice.dao.ChoiceDAOOfAutoHome;
import com.zizo.autohome.kb.crawl.KBCrawlByFileOfAutoHome;
import com.zizo.autohome.kb.crawl.KBCrawlOfAutoHome;
import com.zizo.autohome.kb.dao.KBDaoOfAutoHome;
import com.zizo.autohome.news.post.crawl.NewsPostCrawlOfAutoHome;
import com.zizo.autohome.news.post.dao.NewsDaoOfAutoHome;
import com.zizo.autohome.news.reply.crawl.NewsReplyCrawlOfAutoHome;
import com.zizo.autohome.news.reply.dao.NewsReplyDaoOfAutoHome;
import com.zizo.autohome.price.crawl.PriceCrawlOfAutoHome;
import com.zizo.autohome.price.dao.PriceDaoOfAutoHome;
import com.zizo.autohome.reply.crawl.ReplyCarwlOfAutoHome;
import com.zizo.autohome.reply.dao.ReplyDaoOfAutoHome;
import com.zizo.autohome.user.crawl.UserCrawl;
import com.zizo.autohome.user.dao.UserDAO;
import com.zizo.core.Errors;
import com.zizo.util.ConnectionPool;
import com.zizo.util.Util;

/**
 * @ClassName: MainTemp
 * @Description:
 * @author: Alexis
 * @date: 2016年7月23日 下午2:38:08
 */
public class Main {
	public static void main(String[] args) {

		Main.start("autohome", "price", 15);

		// Main.start("autohome", "bbs_zlfk", 10);

		Main.start("autohome", "carmodel", 20);
	}

	/**
	 * @Title: start
	 * @Description: 开始运行的函数，由Main类中的主方法调用
	 * @param website
	 *            网站，分为 autohome,bitauto,xcar,pcauto
	 * @param model
	 *            网站的子模块
	 * @param threadCount
	 *            线程的数量
	 * @return: void
	 * @author: Alexis
	 */
	public static void start(String website, String model, int threadCount) {
		ExecutorService fixexecutorservice = Executors.newFixedThreadPool(threadCount);
		List<Errors> error = new ArrayList<>();
		Connection currConn;
		Connection coreConn;
		if (website.equals("autohome")) {
			switch (model) {
			case "userID":
				currConn = ConnectionPool.getConnection("autohome_other", "localhost", "3306");
				UserDAO userdao = new UserDAO(currConn);
				/* 2016年11月2日 上午11:25:00 此时的AutoHome的网址注册人数最多为33694190 */
				for (int i = 32775389; i < 33694190; i++) {
					fixexecutorservice.execute(new UserCrawl(i + "", userdao, error));
				}
				break;
			case "userData":
				currConn = ConnectionPool.getConnection("autohome_other", "localhost", "3306");
				UserDAO userDataDAO = new UserDAO(currConn);
				List<String> userIDS = userDataDAO.getIDs();
				System.out.println("getID Finish");
				for (String userID : userIDS) {
					fixexecutorservice.execute(new UserCrawl(userID, userDataDAO, error));
				}
				break;
			case "kb":
				String ip = "localhost";
				currConn = ConnectionPool.getConnection("autohome_other", ip, "3306");
				coreConn = ConnectionPool.getConnection("carvoice_core", ip, "3306");
				KBDaoOfAutoHome kbdao = new KBDaoOfAutoHome(currConn, coreConn);
				List<String> carIDs = kbdao.getIDs();
				Map<String, String> syn = kbdao.getSyn();
				for (String carID : carIDs) {
					fixexecutorservice.execute(new KBCrawlOfAutoHome(carID, kbdao, syn));
				}
				fixexecutorservice.shutdown();
				break;
			case "kbbyfile":// 通过镜像文件爬取
				currConn = ConnectionPool.getConnection("autohome", "localhost");
				coreConn = ConnectionPool.getConnection("carvoice_core", "localhost");
				KBDaoOfAutoHome kbfiledao = new KBDaoOfAutoHome(currConn, coreConn);
				String kbpathname = "/var/server/reflectfile/autohome/kb";// 到口碑的主目录
				List<File> kbfiles = Util.getDirectDirectory(kbpathname);
				for (File kbfile : kbfiles) {
					fixexecutorservice.execute(new KBCrawlByFileOfAutoHome(kbfile.getAbsolutePath(), kbfiledao));
				}
				fixexecutorservice.shutdown();
				break;
			case "price":
				ip = "localhost";
				currConn = ConnectionPool.getConnection("autohome_other", ip, "3306");
				PriceDaoOfAutoHome pricedao = new PriceDaoOfAutoHome(currConn);
				for (int j = 1; j < 71861; j++) {
					fixexecutorservice.execute(new PriceCrawlOfAutoHome(j + "", pricedao));
				}
				break;
			case "reply":
				currConn = ConnectionPool.getConnection("autohome", "localhost", "3306");
				coreConn = ConnectionPool.getConnection("autohome", "192.169.0.198");
				ReplyDaoOfAutoHome replydao = new ReplyDaoOfAutoHome(currConn, coreConn);
				List<String> userids = replydao.getErrorUrl();
				// List<String> finIDs = replydao.getFinishID();
				// userids.removeAll(finIDs);
				System.out.println("共：" + userids.size());
				for (String uid : userids) {
					fixexecutorservice.execute(new ReplyCarwlOfAutoHome(uid, replydao));
				}
				break;
			case "choice":
				ip = "carvoice.tpddns.cn";
				currConn = ConnectionPool.getConnection("autohome", ip, "3308");
				ChoiceDAOOfAutoHome choicedao = new ChoiceDAOOfAutoHome(currConn);
				List<String> choiceIDs = choicedao.getIDs();
				List<String> finIDs = choicedao.getFinID();
				choiceIDs.removeAll(finIDs);
				System.out.println(choiceIDs.size());
				for (String s : choiceIDs) {
					fixexecutorservice.execute(new ChoiceCrawlOfAutoHome(s, choicedao));
				}
				break;
			case "carmodel":
				ip = "localhost";
				currConn = ConnectionPool.getConnection("autohome_other", ip, "3306");
				currConn = ConnectionPool.getConnection();
				CarModelDao carmodeldao = new CarModelDao(currConn);
				List<String> carmodelIDs = carmodeldao.getIDs();
				System.out.println(carmodelIDs.size());
				for (String s : carmodelIDs) {
					fixexecutorservice.execute(new CarModelCrawlOfAutoHome(s, carmodeldao));
				}
				break;
			case "newspost":
				ip = "localhost";
				currConn = ConnectionPool.getConnection("autohome_other", ip, "3306");
				NewsDaoOfAutoHome newspostdao = new NewsDaoOfAutoHome(currConn);
				List<String> newspostids = newspostdao.getIDs();
				for (String i : newspostids) {
					fixexecutorservice.execute(new NewsPostCrawlOfAutoHome(i, newspostdao));
				}
				break;
			case "newsreply":
				ip = "localhost";
				currConn = ConnectionPool.getConnection("autohome", ip, "3306");
				NewsReplyDaoOfAutoHome newsreplydao = new NewsReplyDaoOfAutoHome(currConn, null);
				List<String> ids = new ArrayList<>();
				for (int i = 1; i < 4842; i++) {
					ids.add(i + "");
				}
				List<String> fin = newsreplydao.getFinIDs();
				ids.removeAll(fin);
				for (String replyids : ids) {
					fixexecutorservice.execute(new NewsReplyCrawlOfAutoHome(replyids, newsreplydao));
				}
				break;
			case "bbs_zlfk":
				Connection conn = ConnectionPool.getConnection("autohome_bbs", "localhost", "3306");
				Connection core = ConnectionPool.getConnection("autohome_old", "localhost", "3306");
				ZLFKDaoOfAutoHome dao = new ZLFKDaoOfAutoHome(conn, core);
				List<String> qlfkids = dao.getTables();
				for (String qlfkid : qlfkids) {
					fixexecutorservice.execute(new ZLPJCrawlOfAutoHome(qlfkid, dao));
				}
				break;
			default:
				break;
			}
		}
		fixexecutorservice.shutdown();
	}
}
